<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>积分维护</title>
    </head>
    <body>
        <div id="check_point_form">
        </div>
        <div id="customersPointGrid" class="x-form-item">
        </div>
        <script>

            Ext.onReady(function(){

                var noField = new Ext.form.TextField({
                    fieldLabel: '会员卡号',
                    name: 'customer_no',
                    allowBlank: true,
                    anchor: '99%'
                });
                var phoneField = new Ext.form.TextField({
                    fieldLabel: '联系电话',
                    name: 'phone',
                    allowBlank: true,
                    anchor: '99%'
                });
                var nameField = new Ext.form.TextField({
                    fieldLabel: '姓名',
                    name: 'name',
                    allowBlank: true,
                    anchor: '99%'
                });

                var pointFrom = new Ext.form.NumberField({
                    fieldLabel: '积分从',
                    name: 'p_from',
                    allowBlank: true,
                    width: 50
                });
                var pointTo = new Ext.form.NumberField({
                    fieldLabel: '至',
                    name: 'p_to',
                    allowBlank: true,
                    width: 50
                });
                var priceFrom = new Ext.form.NumberField({
                    fieldLabel: '消费从',
                    name: 'm_from',
                    allowBlank: true,
                    width: 50
                });
                var priceTo = new Ext.form.NumberField({
                    fieldLabel: '至',
                    name: 'm_to',
                    allowBlank: true,
                    width: 50
                });

                var submitButton = new Ext.Button({
                    text: '查询',
                    handler: function(){
                        if (formPanel.form.isValid()) {
                            var from = pointFrom.getValue();
                            var to = pointTo.getValue();
                            if (from != "" && to != "" && parseInt(from) >= parseInt(to)) {
                                Ext.MessageBox.alert('提示', '请检查积分查询范围');
                                return false;
                            }
                            var from_p = priceFrom.getValue();
                            var to_p = priceTo.getValue();
                            if (from_p != "" && to_p != "" && parseInt(from_p) >= parseInt(to_p)) {
                                Ext.MessageBox.alert('提示', '请检查消费查询范围');
                                return false;
                            }
                            customerDS.baseParams.customerNo = noField.getValue();
                            customerDS.baseParams.phone = phoneField.getValue();
                            customerDS.baseParams.name = nameField.getValue();
                            customerDS.baseParams.from = from;
                            customerDS.baseParams.to = to;
                            customerDS.baseParams.from_p = from_p;
                            customerDS.baseParams.to_p = to_p;
                            customerDS.reload();
                        }
                    }
                });
                var countButton = new Ext.Button({
                    text: '重新计算',
                    handler: function(){
                        Ext.Ajax.request({
                            url: '/customers/reCountCustomersPoint',
                            method: 'GET',
                            success: function(result, request){
                                if (result.responseText.indexOf("成功") > 0) {
                                    customerDS.reload();
                                    Ext.MessageBox.alert('操作成功', result.responseText);
                                }
                                else {
                                    Ext.MessageBox.alert('操作信息', result.responseText);
                                }
                            },
                            failure: function(result, request){
                                Ext.MessageBox.alert('操作信息', '提交订票信息失败' + result.responseText);
                            }
                        });
                    }
                });

                var formPanel = new Ext.form.FormPanel({
                    //baseCls: 'x-plain',
                    labelWidth: 55,
                    width: '100%',
                    frame: true,
                    title: '积分查询',
                    labelAlign: 'right',
                    items: [{
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [nameField]
                                }, {
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [noField]
                                }, {
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [phoneField]
                                }]
                        }, {
                            layout: 'column',
                            border: false,
                            items: [{
                                    columnWidth: .12,
                                    layout: 'form',
                                    border: false,
                                    items: [pointFrom]
                                }, {
                                    columnWidth: .15,
                                    layout: 'form',
                                    border: false,
                                    items: [pointTo]
                                }, {
                                    columnWidth: .12,
                                    layout: 'form',
                                    border: false,
                                    items: [priceFrom]
                                }, {
                                    columnWidth: .15,
                                    layout: 'form',
                                    border: false,
                                    items: [priceTo]
                                }, {
                                    columnWidth: .1,
                                    layout: 'form',
                                    border: false,
                                    items: [submitButton]
                                }, {
                                    columnWidth: .2,
                                    layout: 'form',
                                    border: false,
                                    items: [countButton]
                                }]
                        }]
                });

                formPanel.render('check_point_form');

                //############## Grid ##############
                var customerCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                        id: 'id',
                        header: "序号",
                        dataIndex: 'id',
                        width: 50,
                        hidden: true
                    }, {
                        header: "客户名称",
                        width: 100,
                        dataIndex: 'name'
                    }, {
                        header: "性别",
                        width: 50,
                        dataIndex: 'sex',
                        hidden: true
                    }, {
                        header: "电话",
                        width: 100,
                        dataIndex: 'phone',
                        hidden: true
                    }, {
                        header: "证件类型",
                        width: 60,
                        dataIndex: 'card_type',
                        hidden: true
                    }, {
                        header: "证件号码",
                        width: 90,
                        dataIndex: 'card',
                        hidden: true
                    }, {
                        header: "生日",
                        width: 80,
                        dataIndex: 'birthday',
                        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                        hidden: true
                    }, {
                        header: "会员卡号",
                        width: 100,
                        dataIndex: 'customer_no'
                    }, {
                        header: "联系人",
                        width: 80,
                        dataIndex: 'linkman',
                        hidden: true
                    }, {
                        header: "联系人电话",
                        width: 90,
                        dataIndex: 'linkman_phone',
                        hidden: true
                    }, {
                        header: "地址",
                        width: 100,
                        dataIndex: 'address',
                        hidden: true
                    }, {
                        header: "类型",
                        width: 70,
                        dataIndex: 'customer_type'
                    }, {
                        header: "当前积分",
                        width: 100,
                        dataIndex: 'point'
                    }, {
                        header: "累计积分",
                        width: 100,
                        dataIndex: 'point_his'
                    }, {
                        header: "累计消费",
                        width: 100,
                        dataIndex: 'expenditure'
                    }, {
                        header: "历史消费",
                        width: 100,
                        dataIndex: 'expenditure_his'
                    }, {
                        header: "票款合计",
                        width: 100,
                        dataIndex: 'price'
                    }, {
                        header: "税款合计",
                        width: 100,
                        dataIndex: 'tax'
                    }, {
                        header: "附加合计",
                        width: 100,
                        dataIndex: 'fuel'
                    }, {
                        header: "当前积分(历史)",
                        width: 100,
                        dataIndex: 'point_old'
                    }, {
                        header: "累计积分(历史)",
                        width: 100,
                        dataIndex: 'point_his_old'
                    }, {
                        header: "累计消费(历史)",
                        width: 100,
                        dataIndex: 'expenditure_old'
                    }, {
                        header: "历史消费(历史)",
                        width: 100,
                        dataIndex: 'expenditure_his_old'
                    }, {
                        header: "票款合计(历史)",
                        width: 100,
                        dataIndex: 'price_old'
                    }, {
                        header: "税款合计(历史)",
                        width: 100,
                        dataIndex: 'tax_old'
                    }, {
                        header: "附加合计(历史)",
                        width: 100,
                        dataIndex: 'fuel_old'

                    }]);

                var customerDS = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/customers/getCustomersPoint'
                    }),
                    reader: new Ext.data.JsonReader({
                        root: 'root',
                        totalProperty: 'totalProperty'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'name'
                        }, {
                            name: 'sex'
                        }, {
                            name: 'phone'
                        }, {
                            name: 'card_type',
                            mapping: 'card_type.card_type'
                        }, {
                            name: 'card'
                        }, {
                            name: 'birthday'
                        }, {
                            name: 'customer_no'
                        }, {
                            name: 'address'
                        }, {
                            name: 'linkman'
                        }, {
                            name: 'linkman_phone'
                        }, {
                            name: 'customer_type',
                            mapping: 'customer_type.customer_type'
                        }, {
                            name: 'customer_level'
                        }, {
                            name: 'point'
                        }, {
                            name: 'point_his'
                        }, {
                            name: 'expenditure'
                        }, {
                            name: 'expenditure_his'
                        }, {
                            name: 'price'
                        }, {
                            name: 'tax'
                        }, {
                            name: 'fuel'
                        }, {
                            name: 'point_old'
                        }, {
                            name: 'point_his_old'
                        }, {
                            name: 'expenditure_old'
                        }, {
                            name: 'expenditure_his_old'
                        }, {
                            name: 'price_old'
                        }, {
                            name: 'tax_old'
                        }, {
                            name: 'fuel_old'
                        }])
                });

                var customerGridPanel = new Ext.grid.GridPanel({
                    border: false,
                    el: 'customersPointGrid',
                    ds: customerDS,
                    cm: customerCM,
                    iconCls: 'icon-grid',
                    loadMask: true,
                    autoWidth: true,
                    collapsible: true,
                    animCollapse: true,
                    height: 450,
                    //frame: true,
                    title: '客户列表',
                    bbar: new Ext.PagingToolbar({
                        pageSize: 15,
                        displayInfo: true,
                        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                        emptyMsg: "没有记录",
                        store: customerDS
                    })
                });

                customerGridPanel.render();
                customerDS.load({
                    params: {
                        start: 0,
                        limit: 15
                    }
                });

                var point_id = ""; //客户id
                var point_name = ""; //客户姓名
                var point_cno = ""; //会员卡号
                customerGridPanel.on('rowcontextmenu', function(customerGridPanel, rowIndex, e){
                    var srow = customerDS.getById(customerDS.data.items[rowIndex].id);
                    point_id = srow.get("id");
                    point_name = srow.get("name");
                    point_cno = srow.get("customer_no");
                    e.preventDefault();
                    pointRightClick.showAt(e.getXY());
                });

                var pointRightClick = new Ext.menu.Menu({
                    items: [{
                            handler: pointRightMenu1,
                            text: '【积分维护】'
                        }, {
                            handler: pointRightMenu2,
                            text: '【查看记录】'
                        }, "-", {
                            handler: pointRightMenu3,
                            text: '【初始化】'
                        }]
                });

                function pointRightMenu1(){
                    ModPoint(customerDS, point_id, point_name, point_cno);
                }
                function pointRightMenu2(){
                    showPointModHis(point_id, point_name);
                }
                function pointRightMenu3(){
                    ModPointHis(customerDS, point_id, point_name, point_cno);
                }

                function ModPoint(dataStore, id, name, no){
                    var cName = new Ext.form.TextField({
                        fieldLabel: '客户',
                        allowBlank: true,
                        name: 'c_name',
                        readOnly: false,
                        anchor: '90%',
                        value: name
                    });
                    var cNo = new Ext.form.TextField({
                        fieldLabel: '会员卡号',
                        allowBlank: true,
                        name: 'c_no',
                        readOnly: true,
                        anchor: '90%',
                        value: no
                    });
                    var options = new Ext.form.ComboBox({
                        store: new Ext.data.Store({
                            reader: new Ext.data.ArrayReader({}, ['value']),
                            data: [['加'], ['减']]
                        }),
                        name: 'options',
                        fieldLabel: '操作类型',
                        resiable: true,
                        typeAhead: true,
                        width: 50,
                        //handleHeight: 100,
                        displayField: 'value',
                        valueField: 'value',
                        nameField: 'status',
                        selectOnFocus: true,
                        mode: 'local',
                        selectOnFocus: true,
                        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
                        readOnly: true,
                        emptyText: '选择类型...',
                        allowBlank: true,
                        value: '加'
                    });

                    var mPoint = new Ext.form.NumberField({
                        fieldLabel: '操作积分',
                        name: 'point',
                        allowBlank: false,
                        anchor: '90%',
                        value: 0
                    });
                    var mExp = new Ext.form.NumberField({
                        fieldLabel: '操作消费',
                        name: 'expenditure',
                        allowBlank: false,
                        anchor: '90%',
                        value: 0
                    });
                    var mPrice = new Ext.form.NumberField({
                        fieldLabel: '操作票款',
                        name: 'price',
                        allowBlank: false,
                        anchor: '90%',
                        value: 0
                    });
                    var mTax = new Ext.form.NumberField({
                        fieldLabel: '操作税费',
                        name: 'tax',
                        allowBlank: false,
                        anchor: '90%',
                        value: 0
                    });
                    var mFuel = new Ext.form.NumberField({
                        fieldLabel: '操作附加',
                        name: 'fuel',
                        allowBlank: false,
                        anchor: '90%',
                        value: 0
                    });
                    var remark = new Ext.form.TextArea({
                        fieldLabel: '说明',
                        name: 'remark',
                        allowBlank: false,
                        height: 40
                    });

                    var formPanel = new Ext.form.FormPanel({
                        //baseCls: 'x-plain',
                        labelWidth: 60,
                        labelAlign: 'right',
                        frame: true,
                        url: "/customers/saveCustomerPointMod?id=" + id,
                        items: [cName, cNo, options, mPoint, mExp, mPrice, mTax, mFuel, remark]
                    });
                    var modWindow = new Ext.Window({
                        title: '积分维护',
                        width: 260,
                        height: 350,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: formPanel,
                        buttons: [{
                                text: '保存',
                                handler: function(){
                                    if (formPanel.form.isValid()) {
                                        formPanel.form.submit({
                                            waitMsg: 'In processing',
                                            success: function(form, action){
                                                var info = "操作失败，具体失败原因请查询系统日志";
                                                if (action.result && action.result.info && action.result.info != "") {
                                                    info = action.result.info;
                                                }
                                                Ext.MessageBox.alert('操作信息', info);
                                                if (info.indexOf("成功") >= 0) {
                                                    modWindow.hide();
                                                    dataStore.reload();
                                                }
                                            },
                                            failure: function(form, action){
                                                var info = "操作失败，具体失败原因请查询系统日志";
                                                if (action.result && action.result.info && action.result.info != "") {
                                                    info = action.result.info;
                                                }
                                                Ext.MessageBox.alert('操作信息', info);
                                            }
                                        });
                                    }
                                }
                            }, {
                                text: '取消',
                                handler: function(){
                                    modWindow.hide();
                                }
                            }]
                    });
                    modWindow.show();
                };

                function showPointModHis(id, name){
                    var store = new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/customers/getPointHis?id=" + id
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                                name: 'id'
                            }, {
                                name: 'name',
                                mapping: 'customer.name'
                            }, {
                                name: 'options'
                            }, {
                                name: 'point'
                            }, {
                                name: 'expenditure'
                            }, {
                                name: 'price'
                            }, {
                                name: 'tax'
                            }, {
                                name: 'fuel'
                            }, {
                                name: 'user',
                                mapping: 'user.user_name'
                            }, {
                                name: 'created_at'
                            }, {
                                name: 'remark'
                            }])
                    });
                    var grid = new Ext.grid.GridPanel({
                        store: store,
                        loadMask: true,
                        autoWidth: true,
                        columns: [{
                                header: "id",
                                width: 60,
                                dataIndex: 'id',
                                hidden: true
                            }, {
                                header: "客户",
                                width: 60,
                                dataIndex: 'name'
                            }, {
                                header: "操作类型",
                                width: 60,
                                sortable: true,
                                dataIndex: 'options'
                            }, {
                                header: "积分",
                                width: 50,
                                sortable: true,
                                dataIndex: 'point'
                            }, {
                                header: "消费",
                                width: 50,
                                sortable: true,
                                dataIndex: 'expenditure'
                            }, {
                                header: "税费",
                                width: 50,
                                sortable: true,
                                dataIndex: 'tax'
                            }, {
                                header: "附加",
                                width: 50,
                                sortable: true,
                                dataIndex: 'fuel'
                            }, {
                                header: "票价",
                                width: 50,
                                sortable: true,
                                dataIndex: 'price'
                            }, {
                                header: "经办人",
                                width: 60,
                                sortable: true,
                                dataIndex: 'user'
                            }, {
                                header: "操作时间",
                                width: 120,
                                sortable: true,
                                dataIndex: 'created_at',
                                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
                            }, {
                                header: "说明",
                                width: 100,
                                dataIndex: 'remark'
                            }],
                        stripeRows: true
                    });

                    var hisWindow = new Ext.Window({
                        title: '维护历史查看',
                        width: 680,
                        height: 350,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: [grid],
                        buttons: [{
                                text: '删除',
                                handler: function(){
                                    var m = grid.getSelections();
                                    if (m.length > 0) {
                                        Ext.MessageBox.confirm('提示', '你真的要删除这些记录吗？', function(btn){
                                            if (btn == 'yes') {
                                                var jsonData = "[";
                                                for (var i = 0; i < m.length; i++) {
                                                    var ss = "{\"id\":\"" + m[i].get("id") + "\"}";
                                                    if (i == 0) {
                                                        jsonData = jsonData + ss;
                                                    }
                                                    else {
                                                        jsonData = jsonData + "," + ss;
                                                    }
                                                    store.remove(m[i]);
                                                }
                                                jsonData = jsonData + "]";
                                                store.load({
                                                    params: {
                                                        delData: jsonData
                                                    }
                                                });
                                            }
                                        });

                                    }
                                }
                            }, {
                                text: '关闭',
                                handler: function(){
                                    hisWindow.hide();
                                }
                            }]
                    });
                    hisWindow.show();

                    store.load();
                }

            })
        </script>
    </body>
</html>
